package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicListChaperController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonChapterRepository chapterRepository;

    @Autowired
    YxyLessonsTopicLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/chapter/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        List<YxyLessonsTopicLessonChapter> chapterList = chapterRepository.findByLessonIdOrderByOrderNum(lessonId);

        List<Chapter> list = new ArrayList<Chapter>();

        Integer chapterOrderNum = 1;

        for (YxyLessonsTopicLessonChapter chapter : chapterList) {
            Chapter ch = new Chapter();
            ch.chapterId = chapter.getId();
            ch.title = chapter.getTitle();
            ch.intro = chapter.getIntro();
            ch.chapterOrderNum = chapterOrderNum;

            List<YxyLessonsTopicLessonItem> itemList = itemRepository.findByChapterIdAndStatusNotOrderByOrderNum(chapter.getId(), -1);
            List<Item> items = new ArrayList<>();
            for (YxyLessonsTopicLessonItem item : itemList) {
                Item i = new Item();
                i.itemId = item.getId();
                i.title = item.getTitle();
                i.intro = item.getIntro();
                i.status = item.getStatus() + "";
                i.orderNum = item.getOrderNum();
                i.rejectReason = item.getRejectReason();
                String videoId = item.getVideoId();
                if (StringUtils.isNotBlank(videoId)) {
                    YxyLessonsVideo video = videoRepository.findById(videoId);
                    if (null != video) {
                        i.duration = disDuration(video.getDuration());
                    }
                }
                items.add(i);
            }
            ch.itemList = items;
            list.add(ch);
            chapterOrderNum++;
        }
        return new RespModel(list);
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }

    public static class Item {
        public String itemId;
        public String title;
        public String intro;
        public String status;
        public String duration;
        public Integer orderNum;
        public String rejectReason;
    }

    public static class Chapter {
        public String chapterId;
        public String title;
        public String intro;
        public Integer chapterOrderNum;
        public List<Item> itemList;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "课程不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public List<Chapter> list;

        public RespModel(List<Chapter> list) {
            this.list = list;
        }
    }
}
